package cn.dao.claim.impl;

import cn.dao.claim.ClaimVoucherDao;
import cn.entity.ClaimVoucher;
import cn.entity.Employee;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

@Repository("claimVoucherDao")
public class ClaimVoucherDaoImpl extends HibernateDaoSupport implements ClaimVoucherDao {

    public ClaimVoucherDaoImpl() {
    }
    @Autowired
    public ClaimVoucherDaoImpl(@Qualifier("sessionFactory")SessionFactory sessionFactory){
        this.setSessionFactory(sessionFactory);
    }

    @Override
    public List<ClaimVoucher> findBySnAndPage(final Employee employee, final int indexPage, final int pageSize,final String status,final Date startDate,final Date endDate) {
        List<ClaimVoucher> claimVouchers = (List<ClaimVoucher>) getHibernateTemplate().execute(
                new HibernateCallback() {

                    @Override
                    public Object doInHibernate(Session session) throws HibernateException, SQLException {
                        String hql = "from ClaimVoucher c ";
                        if (employee.getSysPosition().getId()==2||employee.getSysPosition().getId()==3){
                            hql += " where c.nextDeal.sn=?  and c.status!='新创建'";
                        }else if (employee.getSysPosition().getId()== 4){
                            hql += " where c.nextDeal.sn=?  and c.status='已审批'";
                        }else {
                            hql += " where c.creator.sn=?";
                        }


                        if (status!=null && !"".equals(status)){
                            hql += " and c.status=:status";
                        }
                        if (startDate!=null && !"".equals(startDate)){
                            hql += " and createTime>=:startDate";
                        }
                        if (endDate!=null && !"".equals(endDate)){
                            hql += " and createTime<=:endDate";
                        }
                        hql +=" order by status asc,createTime desc ";
                        Query query = session.createQuery(hql);

                        if (status!=null && !"".equals(status)){
                            query.setString("status",status);
                        }
                        if (startDate!=null && !"".equals(startDate)){
                            query.setDate("startDate",startDate);
                        }
                        if (endDate!=null && !"".equals(endDate)){
                            query.setDate("endDate",endDate);
                        }
                        int begin = (indexPage-1)*pageSize;

                        return query.setString(0,employee.getSn()).setFirstResult(begin).setMaxResults(pageSize).list();
                    }
                }
        );
        return claimVouchers;
    }

    @Override
    public long findCountBySn(final Employee employee, final String status, final Date startDate, final Date endDate) {
        long count = getHibernateTemplate().execute(new HibernateCallback<Long>() {
            @Override
            public Long doInHibernate(Session session) throws HibernateException, SQLException {
                String hql = "select  count(id) from ClaimVoucher c ";
                if (employee.getSysPosition().getId()==2||employee.getSysPosition().getId()==3||employee.getSysPosition().getId()==4){
                    hql += " where c.nextDeal.sn=? and c.status!='新创建'";
                }else {
                    hql += " where c.creator.sn=?";
                }
                if (status!=null && !"".equals(status)){
                    hql += " and c.status=:status";
                }
                if (startDate!=null && !"".equals(startDate)){
                    hql += " and createTime>=:startDate";
                }
                if (endDate!=null && !"".equals(endDate)){
                    hql += " and createTime<=:endDate";
                }
                Query query = session.createQuery(hql);
                if (status!=null && !"".equals(status)){
                    query.setString("status",status);
                }
                if (startDate!=null && !"".equals(startDate)){
                    query.setDate("startDate",startDate);
                }
                if (endDate!=null && !"".equals(endDate)){
                    query.setDate("endDate",endDate);
                }
                return (long) query.setString(0,employee.getSn()).uniqueResult();
            }
        });
        return count;
    }

    @Override
    public ClaimVoucher findById(Long id) {
        return getHibernateTemplate().get(ClaimVoucher.class,id);
    }



    @Override
    public void update(ClaimVoucher claimVoucher) {
        getHibernateTemplate().saveOrUpdate(claimVoucher);
    }

    @Override
    public void delete(ClaimVoucher claimVoucher) {
        getHibernateTemplate().delete(claimVoucher);
    }
}
